﻿using SkiaSharp;
using SkiaSharp.Views.Desktop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormApp
{
    public partial class FTestSKCanvas : Form
    {
        public FTestSKCanvas()
        {
            InitializeComponent();
        }

        private void FTestSKCanvas_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int fontSize = (int)numericUpDown1.Value;
            bool topDock = checkBox1.Checked;
            //testRight(fontSize, topDock);
            testLeft(fontSize, topDock);
        }

        private void testLeft(int fontSize, bool topDock)
        {
            using var bitmap = new SKBitmap(600, 400);
            using var currentCanvas = new SKCanvas(bitmap);
            currentCanvas.Clear(SKColors.Gray);
            using var testPaint = new SKPaint()
            {
                TextSize = fontSize,
                IsAntialias = true,
                TextAlign = SKTextAlign.Center,
                Typeface = SKTypeface.FromFamilyName("Arial"),
                FilterQuality = SKFilterQuality.High,
                IsStroke = true,
                StrokeWidth = 1
            };

            string timeStr = "2025-1-12 12:35:50";
            var size = new SKRect();
            testPaint.MeasureText(timeStr, ref size);





            //SKPoint point1, point2;

            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(600, 0);
            //testPaint.Color = SKColors.Red;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(0, 400);
            //testPaint.Color = SKColors.Green;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(600, 0);
            //point2 = new SKPoint(600, 400);
            //testPaint.Color = SKColors.Blue;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 400);
            //point2 = new SKPoint(600, 400);
            //testPaint.Color = SKColors.Yellow;
            //currentCanvas.DrawLine(point1, point2, testPaint);


            //currentCanvas.Translate(bitmap.Height, 0);
            //currentCanvas.RotateDegrees(90, bitmap.Height, 0);

            currentCanvas.RotateDegrees(-90, 0, 0);
            currentCanvas.Translate(-bitmap.Height, 0);
            currentCanvas.Save();


            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(400, 0);
            //testPaint.Color = SKColors.Red;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(0, 600);
            //testPaint.Color = SKColors.Green;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(400, 0);
            //point2 = new SKPoint(400, 600);
            //testPaint.Color = SKColors.Blue;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 600);
            //point2 = new SKPoint(400, 600);
            //testPaint.Color = SKColors.Yellow;
            //currentCanvas.DrawLine(point1, point2, testPaint);


            testPaint.Color = SKColors.Black;
            float x;
            if (topDock)
            {
                x = bitmap.Height - size.Width / 2;//下
            }
            else
            {
                x = size.Width / 2;//上
            }

            timeStr = "2025-1-12 12:35:50";
            currentCanvas.DrawText(timeStr, x, 30, testPaint);

            timeStr = "2025-1-12 12:35:54";
            currentCanvas.DrawText(timeStr, x, 80, testPaint);

            timeStr = "2025-1-12 12:35:48";
            currentCanvas.DrawText(timeStr, x, 550, testPaint);

            currentCanvas.Flush();

            using var bitmap2 = bitmap.ToBitmap();
            using var graphics = pictureBox1.CreateGraphics();
            graphics.DrawImage(bitmap2, 0, 0);
        }


        private void testRight(int fontSize, bool topDock)
        {
            using var bitmap = new SKBitmap(600, 400);
            using var currentCanvas = new SKCanvas(bitmap);
            currentCanvas.Clear(SKColors.Gray);
            using var testPaint = new SKPaint()
            {
                TextSize = fontSize,
                IsAntialias = true,
                TextAlign = SKTextAlign.Center,
                Typeface = SKTypeface.FromFamilyName("Arial"),
                FilterQuality = SKFilterQuality.High,
                IsStroke = true,
                StrokeWidth = 1
            };

            string timeStr = "2025-1-12 12:35:50";
            var size = new SKRect();
            testPaint.MeasureText(timeStr, ref size);





            //SKPoint point1, point2;

            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(600, 0);
            //testPaint.Color = SKColors.Red;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(0, 400);
            //testPaint.Color = SKColors.Green;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(600, 0);
            //point2 = new SKPoint(600, 400);
            //testPaint.Color = SKColors.Blue;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 400);
            //point2 = new SKPoint(600, 400);
            //testPaint.Color = SKColors.Yellow;
            //currentCanvas.DrawLine(point1, point2, testPaint);


            //currentCanvas.Translate(bitmap.Height, 0);
            //currentCanvas.RotateDegrees(90, bitmap.Height, 0);

            //currentCanvas.RotateDegrees(-90, 0, 0);
            //currentCanvas.Translate(-bitmap.Height, 0);
            currentCanvas.RotateDegrees(90, 0, 0);
            currentCanvas.Translate(0, -bitmap.Width);
            currentCanvas.Save();


            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(400, 0);
            //testPaint.Color = SKColors.Red;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 0);
            //point2 = new SKPoint(0, 600);
            //testPaint.Color = SKColors.Green;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(400, 0);
            //point2 = new SKPoint(400, 600);
            //testPaint.Color = SKColors.Blue;
            //currentCanvas.DrawLine(point1, point2, testPaint);

            //point1 = new SKPoint(0, 600);
            //point2 = new SKPoint(400, 600);
            //testPaint.Color = SKColors.Yellow;
            //currentCanvas.DrawLine(point1, point2, testPaint);


            testPaint.Color = SKColors.Black;
            float x;
            if (topDock)
            {
                x = size.Width / 2;//上
            }
            else
            {
                x = bitmap.Height - size.Width / 2;//下
            }

            timeStr = "2025-1-12 12:35:50";
            currentCanvas.DrawText(timeStr, x, 30, testPaint);

            timeStr = "2025-1-12 12:35:54";
            currentCanvas.DrawText(timeStr, x, 80, testPaint);

            timeStr = "2025-1-12 12:35:48";
            currentCanvas.DrawText(timeStr, x, 550, testPaint);

            currentCanvas.Flush();

            using var bitmap2 = bitmap.ToBitmap();
            using var graphics = pictureBox1.CreateGraphics();
            graphics.DrawImage(bitmap2, 0, 0);
        }

    }
}
